package com.marswin89.marsdaemon.demo;

import android.content.Context;
import android.provider.Settings;
import android.telephony.TelephonyManager;

import java.lang.reflect.Method;
import java.security.MessageDigest;

public class WID {

    /**
     * 计算手机唯一标志wid
     *
     * @return wid的hex编码，小写
     */
    public static String getWid(Context context) {
        if (mWid != null) {
            return mWid;
        }

        mWid = calcWid(context).substring(0, 7) + "_V2.0";
        return mWid;
    }

    private static synchronized String calcWid(Context ctx) {
        if (mWid != null)
            return mWid;

        String s = getImei(ctx) + getAndroidId(ctx) + getSerialNumber();
        mWid = md5(s.getBytes());

        return mWid;
    }

    /**
     * 获取Imei号
     *
     * @param ctx
     * @return
     */
    public static String getImei(Context ctx) {
        TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
        try {
            String id = null;
            if (tm != null && (id = tm.getDeviceId()) != null) {
                return id;
            }
        } catch (Exception e) {
            if (DEBUG) {
                e.printStackTrace();
            }
        }
        return DEFAULT_IMEI;
    }

    /**
     * 获取android的Id
     *
     * @param context
     * @return id或者空串
     */
    public static synchronized String getAndroidId(Context context) {
        String androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);

        return (androidId != null) ? androidId : "";
    }

    /**
     * 获取android序列号
     *
     * @return id或者空串
     */
    public static synchronized String getSerialNumber() {
        String serialNumber = null;
        try {
            Class<?> clazz = Class.forName("android.os.SystemProperties");
            if (clazz != null) {
                Method method_get = clazz.getMethod("get", String.class, String.class);
                if (method_get != null) {
                    serialNumber = (String) (method_get.invoke(clazz, "ro.serialno", ""));
                }
            }
        } catch (Exception e) {
            if (DEBUG) {
                e.printStackTrace();
            }
        }

        return serialNumber != null ? serialNumber : "";
    }

    /**
     * 计算md5，并hex编码
     *
     * @param bytes
     * @return md5字节hex编码后的格式
     */
    public static String md5(byte[] bytes) {

        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(bytes);
            byte[] digest = md.digest();

            return bytesToHex(digest);
        } catch (Exception e) {

            if (DEBUG) {
                e.printStackTrace();
            }
        }
        return "";
    }


    public static String bytesToHex(byte[] bytes) {

        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }

        return new String(hexChars);
    }

    private WID() {
    }

    private static String mWid;
    private static final boolean DEBUG = false;
    private static final String DEFAULT_IMEI = "DEFAULT_IMEI";
    private static final char[] hexArray = "0123456789abcdef".toCharArray();

}
